<?php

/**
 * Users Model
 *
 * Interacts with the users table in the database.
 *
 * @author David Beard
 * @version 1.0
 * @link https://code.google.com/p/lis3468final/
 */
class Users_model extends Model {

		/**
		 * Name of Function: Constructor
		 *
		 * Detailed Description: This is my constructor
		 * that automatically loads when this class is instantiated
		 *
		 * @author David
		 */
    function Users_model()
    {
        parent::Model();

        $this->load->database();
    }

		/**
		 * List Users
		 *
		 * Returns a list of users from the database.
		 *
		 * @return array
		 */
		function list_users()
		{
			$query = $this->db->get('User');

			return $query->result();
		}

		function get_users()
		{
			//get the records the database
			// & convert them into an array.
			$array = array(
				array('name' => "David", 'Email' => 'dmb07@fsu.edu'),
				array('name' => "Leo", 'Email' => 'lcd07c@fsu.edu')
			);

			return $array;
		}

		/*function retrieve_user($user_id)
		{
			//"SELECT * FROM users WHERE `id` = '$user_id'";
			$result = $this->db->get_where('users', array('id' => $user_id));

			if ($result->num_rows() == 1)
				return $result->row();
			else
				return FALSE;
		}

		/**
		 * Add User
		 *
		 * Adds a user to the database.
		 *
		 * @return boolean
		 */
		function add_user()
		{
			//Load encryption
			//$this->load->library('encrypt');

			//Get the values
			$data = array();
			$data['username'] = $this->input->post('username');
			$data['email'] = $this->input->post('email');
			$data['password'] = $this->input->post('password'); //$this->encrypt->sha1($password);
			

			//Try it...
			$result = $this->db->insert('User', $data);
			return $result;
		}

	/*	function check_credentials($username, $password)
		{
			$this->load->library('encrypt');

			//"SELECT * FROM users WHERE `name` = '$un';";
			$query = $this->db->get_where('users', "`name` = '$username'");

			if ($query->num_rows() == 1)
			{
				//good we got a user!
				$password = $this->encrypt->sha1($password);
				$result = $query->row();
				if ($result->password == $password)
					return $result->id;
				else
					return FALSE;
			}
			else
				return FALSE;
		}*/

            function validate()
            {
		$this->db->where('username', $this->input->post('username'));
		$this->db->where('password', ($this->input->post('password')));
		$query = $this->db->get('User');

		if($query->num_rows > 1)
                {
                        return FALSE;
                }
                    
                elseif($query->num_rows == 1)
                {
                        return TRUE;
                }


            }
}
/* EOF */